package com.changpay.zywy.ui.fragment.mine

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.AwardMessageAdapter
import com.changpay.zywy.adapter.SystemMessageAdapter
import com.changpay.zywy.adapter.TixianMessageAdapter
import com.changpay.zywy.base.BaseMvpFragment
import com.changpay.zywy.mvp.contract.MessageContract
import com.changpay.zywy.bean.JiangLiMessageBean
import com.changpay.zywy.bean.SystemMessageBean
import com.changpay.zywy.bean.TiXianMessageBean
import com.changpay.zywy.mvp.presenter.MessagePresenter
import com.changpay.zywy.ui.activity.mine.SystemMessageDetailActivity
import com.changpay.zywy.ui.activity.mine.TiXianMessageDetailActivity
import com.changpay.zywy.wight.CustomLoadMoreView
import kotlinx.android.synthetic.main.fragment_refresh_layout.*


class MessageFragment : BaseMvpFragment<MessageContract.View, MessageContract.Presenter>(), MessageContract.View {

    private var page = 1
    private val pagesize = "10"

    override fun showTixanMessage(data: List<TiXianMessageBean>) {
        data.let {
            mAdapter_one.run {
                addData(it)
                val size = it.size
                if (size < 10) {
                    loadMoreEnd(true)
                } else {
                    loadMoreComplete()
                }
            }
        }
    }

    override fun showAwardMessage(data: List<JiangLiMessageBean>) {
        data.let {
            mAdapter_two.run {
                addData(it)
                val size = it.size
                if (size < 10) {
                    loadMoreEnd(true)
                } else {
                    loadMoreComplete()
                }
            }
        }
    }

    override fun showSystemMessage(data: List<SystemMessageBean>) {
        data.let {
            mAdapter_three.run {
                addData(it)
                val size = it.size
                if (size < 10) {
                    loadMoreEnd(true)
                } else {
                    loadMoreComplete()
                }
            }
        }
    }

    private var position: Int = 0  // 当前viewPage的位置
    private lateinit var type: String


    companion object {
        fun getInstance(instanceType: Int): MessageFragment {
            val bundle = Bundle()
            bundle.putInt("position", instanceType)
            val fragment = MessageFragment()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun createPresenter(): MessageContract.Presenter = MessagePresenter()

    override fun attachLayoutRes(): Int = R.layout.fragment_refresh_layout

    override fun lazyLoad() {
        loadData()
    }

    fun loadData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["page"] = page.toString()
        param["pagesize"] = pagesize
        when (position) {
            0 -> mPresenter?.getTixianMessage(param)
            1 -> mPresenter?.getAwardMessage(param)
            2 -> mPresenter?.getSystemMessage(param)
        }
    }

    override fun initView(view: View) {
        super.initView(view)
        position = arguments?.getInt("position") ?: 0
        swipeRefreshLayout.isEnabled = false
        initRecyclerView()
    }

    /**
     * datas
     */
    private val datas_one = mutableListOf<TiXianMessageBean>()
    private val datas_two = mutableListOf<JiangLiMessageBean>()
    private val datas_three = mutableListOf<SystemMessageBean>()

    /**
     * Adapter
     */
    private val mAdapter_one: TixianMessageAdapter by lazy {
        TixianMessageAdapter(context, datas_one)
    }
    private val mAdapter_two: AwardMessageAdapter by lazy {
        AwardMessageAdapter(context, datas_two)
    }
    private val mAdapter_three: SystemMessageAdapter by lazy {
        SystemMessageAdapter(context, datas_three)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }

    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {

        when (position) {
            0 -> {
                page = mAdapter_one.data.size / 10 + 1
                loadData()
            }
            1 -> {
                page = mAdapter_two.data.size / 10 + 1
                loadData()
            }
            2 -> {
                page = mAdapter_three.data.size / 10 + 1
                loadData()
            }
        }

    }


    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        when (this.position) {
            0 -> {
                if (datas_one.size != 0) {
                    val data = datas_one[position]
                    Intent(activity, TiXianMessageDetailActivity::class.java).run {
                        putExtra("pdc_id", data.pdc_id.toString())
                        startActivity(this)
                    }
                }
            }
            1 -> {

                if (datas_two.size != 0) {
                    val data = datas_two[position]
//            Intent(activity, ContentActivity::class.java).run {
//                putExtra(Constant.CONTENT_URL_KEY, data.link)
//                putExtra(Constant.CONTENT_TITLE_KEY, data.title)
//                putExtra(Constant.CONTENT_ID_KEY, data.id)
//                startActivity(this)
//            }
                }

            }
            2 -> {
                if (datas_three.size != 0) {
                    val data = datas_three[position]
                    Intent(activity, SystemMessageDetailActivity::class.java).run {
                        putExtra("time", data.push_time)
                        putExtra("content", data.push_message)
                        putExtra("title", data.push_title)
                        startActivity(this)
                    }
                }

            }
        }
    }

    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->

    }


    private fun initRecyclerView() {

        recyclerView.run {
            layoutManager = linearLayoutManager
            when (position) {
                0 -> adapter = mAdapter_one
                1 -> adapter = mAdapter_two
                2 -> adapter = mAdapter_three
            }
            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }


        when (position) {
            0 -> {
                mAdapter_one.run {
                    bindToRecyclerView(recyclerView)
                    setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
                    setLoadMoreView(CustomLoadMoreView())
                    onItemClickListener = this@MessageFragment.onItemClickListener
                    setEmptyView(R.layout.fragment_empty_layout)
                }
            }
            1 -> {
                mAdapter_two.run {
                    bindToRecyclerView(recyclerView)
                    setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
                    setLoadMoreView(CustomLoadMoreView())
                    onItemClickListener = this@MessageFragment.onItemClickListener
                    setEmptyView(R.layout.fragment_empty_layout)
                }
            }
            2 -> {
                mAdapter_three.run {
                    bindToRecyclerView(recyclerView)
                    setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
                    setLoadMoreView(CustomLoadMoreView())
                    onItemClickListener = this@MessageFragment.onItemClickListener
                    setEmptyView(R.layout.fragment_empty_layout)
                }

            }
        }


    }
}






